package stsj.meddroid.screen;

import stsj.meddroid.MainAppListener;
import stsj.meddroid.Resources;
import stsj.meddroid.Settings;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actors.Button;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.badlogic.gdx.scenes.scene2d.actors.Button.ClickListener;

public class MainMenuScreen implements Screen, InputProcessor {

	private MainAppListener mainListener;

	private int result = 0;
	
	private Stage stage;
	private Stage exitDialog;
	
	private Button playButton;
	private Button viroButton;
	private Button soundButtonOn;
	private Button soundButtonOff;
	private Button creditButton;
	private Image background;

	private Button exitButtonYes;
	private Button exitButtonNo;
	private Image bgDialog;
	private boolean dialogShow = false;


	private SpriteBatch batch;

	// ===

	// 

	public MainMenuScreen(MainAppListener mainListener) {
		// TODO Auto-generated constructor stub
//		Gdx.app.log("Main Menu ", "Constructor");

		// //
		// =================================================================================
		// =============================================================================
		// =============================================================================
		// =============================================================================

		
		// =============================================================================
		// =============================================================================
		// =============================================================================
		// =============================================================================
		//						

		this.mainListener = mainListener;
		batch = Resources.batch;
		
		if (mainListener.splashScreen != null)
			mainListener.splashScreen.dispose();

		exitDialog = new Stage(480, 320, true);

		bgDialog = new Image("Background", Resources.exitDialogRegion);
		bgDialog.x = exitDialog.width() * 0.25f;
		bgDialog.y = exitDialog.height() / 2 - 60;

		exitButtonYes = new Button("New Button", Resources.yesRegion[0],
				Resources.yesRegion[1]);
		exitButtonNo = new Button("New Button", Resources.noRegion[0],
				Resources.noRegion[1]);

		exitButtonNo.x = exitDialog.width() / 2 + 45;
		exitButtonNo.y = bgDialog.y + 25;
		exitButtonNo.clickListener = new ClickListener() {
			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
//				Gdx.app.log("Play Button", "Clicked");
				dialogShow = false;

			}
		};
		
		exitButtonYes.x = exitButtonNo.x - 10 - exitButtonYes.width;
		exitButtonYes.y = bgDialog.y + 25;
		exitButtonYes.clickListener = new ClickListener() {
			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
//				Gdx.app.log("Play Button", "Clicked");
				Resources.playSound(Resources.clickSound);
				exitGame();
			}
		};


		exitDialog.addActor(bgDialog);
		exitDialog.addActor(exitButtonYes);
		exitDialog.addActor(exitButtonNo);

		stage = new Stage(480, 320, true);

		background = new Image("Background", Resources.backgroundRegion);
		playButton = new Button("New Button", Resources.playRegion[0],
				Resources.playRegion[1]);
		playButton.x = stage.width() * 0.20f;
		playButton.y = stage.height() / 6f;
		playButton.clickListener = new ClickListener() {
			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
//				Gdx.app.log("Play Button", "Clicked");
				Resources.playSound(Resources.clickSound);
				toStageScreen();
			}
		};

		viroButton = new Button("Viro Button", Resources.viroRegion[0],
				Resources.viroRegion[1]);
		viroButton.x = stage.width() / 2 + 15;
		viroButton.y = stage.height() / 6f;
		viroButton.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
//				Gdx.app.log("Viro Button", "Clicked");
				Resources.playSound(Resources.clickSound);
				toViroScreen();
			}

		};

		creditButton = new Button("Credit Button", Resources.creditRegion[0],
				Resources.creditRegion[1]);
		creditButton.x = stage.width() / 12 - 20;
		creditButton.y = stage.height() / 20;
		creditButton.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
//				Gdx.app.log("Credit Button", "Clicked");
				Resources.playSound(Resources.clickSound);
				toCreditScreen();
			}

		};

		soundButtonOn = new Button("SoundOn", Resources.soundRegion[0],
				Resources.soundRegion[1]);
		soundButtonOn.x = stage.width() - soundButtonOn.width - 5;
		soundButtonOn.y = creditButton.y ;
		soundButtonOn.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
//				Gdx.app.log("Sound Button", "Clicked");
				stage.removeActor(soundButtonOn);
				stage.addActor(soundButtonOff);
				soundButtonOff.clearActions();
				Settings.toggleSound();

			}
		};

		soundButtonOff = new Button("SoundOff", Resources.soundRegion[1],
				Resources.soundRegion[0]);
		soundButtonOff.x = soundButtonOn.x;
		soundButtonOff.y = soundButtonOn.y;
		soundButtonOff.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
//				Gdx.app.log("Sound Button", "Clicked");
				stage.removeActor(soundButtonOff);
				stage.addActor(soundButtonOn);
				soundButtonOn.clearActions();
				Settings.toggleSound();

			}
		};

		stage.addActor(background);

		stage.addActor(playButton);
		stage.addActor(viroButton);
		stage.addActor(creditButton);

		if (Settings.soundEnabled) {
			stage.addActor(soundButtonOn);
		} else {
			stage.addActor(soundButtonOff);
		}
		
		
	}

	private void toStageScreen() {
		mainListener.setState(MainAppListener.STAGE_SELECTION);
	}

	private void toViroScreen() {
		mainListener.setState(MainAppListener.VIRO);
	}

	private void toCreditScreen() {
		// TODO Auto-generated method stub
		mainListener.setState(MainAppListener.CREDIT);

	}

	// screen
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Main Menu ", "dispose");
		stage.dispose();

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Main Menu ", "hide");
		dialogShow = false;
	}

	@Override
	public void pause() {
		// TODO Auto-generated meth od stub
//		Gdx.app.log("Main Menu ", "pause");
	}

	@Override
	public void render(float arg0) {

		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30.0f));
		stage.draw();

		if (dialogShow) {
			exitDialog.draw();
		}
		
		if (Settings.theEnd){
			batch.begin();
			if (result != 0)
				batch.draw(Resources.starMedal[result], 480 - 64 - 10, 320 - 64 - 10);
			batch.end();
		}



	}

	@Override
	public void resize(int arg0, int arg1) {

//		Gdx.app.log("Main Menu ", "resize");
	}

	@Override
	public void resume() {
//		Gdx.app.log("Main Menu ", "resume");
		
	}

	@Override
	public void show() {
		// setiap show setInputProcessor..jangan lupa
		
		result = Settings.getResult();
		if (!Resources.bgmOpening.isPlaying())
			Resources.playMusic(Resources.bgmOpening);
		
		stage.clear();
		stage.addActor(background);

		stage.addActor(playButton);
		stage.addActor(viroButton);
		stage.addActor(creditButton);

		if (Settings.soundEnabled) {
			stage.addActor(soundButtonOn);
		} else {
			stage.addActor(soundButtonOff);
		}
		
		if (Settings.theEnd){
			batch.begin();
			if (result != 0)
				batch.draw(Resources.starMedal[result], 480 - 64 - 10, 320 - 64 - 10);
			batch.end();
			
		}
		
		
//		Gdx.app.log("Main Menu ", "show");
		Gdx.input.setCatchBackKey(false); // setcatchbackkey...
		Gdx.input.setInputProcessor(this);

		stage.draw();
	}

	// input processor
	@Override
	public boolean keyDown(int arg0) {


		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {

		return false;
	}

	@Override
	public boolean keyUp(int arg0) {

		if (arg0 == Input.Keys.BACK) {
//			Gdx.app.log("Dialog back", "Dipencet");
			Resources.playSound(Resources.backSound);
			if (dialogShow) {
				dialogShow = false;

			} else
				dialogShow = true;
			return false;
		} 
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {

		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {

		if (dialogShow) {
			exitDialog.touchDown(arg0, arg1, arg2, arg3);
		} else {
			stage.touchDown(arg0, arg1, arg2, arg3);
		}
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {

		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {

		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {

		if (dialogShow) {
			exitDialog.touchUp(arg0, arg1, arg2, arg3);
		} else {
			stage.touchUp(arg0, arg1, arg2, arg3);
		}
		return false;
	}

	private void exitGame() {
		dispose();
		mainListener.exitGame();
	}


}
